package cn.codeforfun.gitee.api;

import cn.codeforfun.gitee.api.email.GiteeEmailService;
import cn.codeforfun.gitee.api.email.GiteeEmailServiceImpl;
import cn.codeforfun.gitee.api.enterprise.GiteeEnterpriseService;
import cn.codeforfun.gitee.api.enterprise.GiteeEnterpriseServiceImpl;
import cn.codeforfun.gitee.api.exception.GiteeRequestException;
import cn.codeforfun.gitee.api.oauth.GiteeOauthService;
import cn.codeforfun.gitee.api.oauth.GiteeOauthServiceImpl;
import cn.codeforfun.gitee.api.repo.GiteeRepoService;
import cn.codeforfun.gitee.api.repo.GiteeRepoServiceImpl;
import cn.codeforfun.gitee.bean.GiteeToken;
import cn.codeforfun.gitee.config.GiteeConfigStorage;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.io.IOException;
import java.util.Set;

/**
 * @author wangbin
 */
public class GiteeServiceImpl implements GiteeService {
    private GiteeConfigStorage giteeConfigStorage;
    private GiteeToken giteeToken;
    private final GiteeRepoService giteeRepoService = new GiteeRepoServiceImpl(this);
    private final GiteeOauthService giteeOauthService = new GiteeOauthServiceImpl(this);
    private final GiteeEmailService giteeEmailService = new GiteeEmailServiceImpl(this);
    private final GiteeEnterpriseService giteeEnterpriseService = new GiteeEnterpriseServiceImpl(this);
    private Validator validator;

    @Override
    public GiteeToken getToken() throws IOException {
        if (null == giteeToken) {
            this.giteeToken = this.giteeOauthService.getTokenByPassword();
        } else if (giteeToken.isExpired()) {
            refreshToken();
        }
        return this.giteeToken;
    }

    @Override
    public void refreshToken() {

    }

    @Override
    public GiteeConfigStorage getGiteeConfigStorage() {
        return this.giteeConfigStorage;
    }

    @Override
    public void setGiteeConfigStorage(GiteeConfigStorage giteeConfigStorage) {
        this.giteeConfigStorage = giteeConfigStorage;
    }

    @Override
    public GiteeEmailService getEmailService() {
        return this.giteeEmailService;
    }

    @Override
    public GiteeEnterpriseService getEnterpriseService() {
        return this.giteeEnterpriseService;
    }

    @Override
    public GiteeOauthService getOauthService() {
        return this.giteeOauthService;
    }

    @Override
    public GiteeRepoService getRepositoryService() {
        return this.giteeRepoService;
    }

    @Override
    public Validator getValidator() {
        if (null == this.validator) {
            this.validator = Validation.byDefaultProvider().configure().addProperty("hibernate.validator.fail_fast", "true").buildValidatorFactory().getValidator();
        }
        return this.validator;
    }

    @Override
    public void validate(Object request) {
        Set<ConstraintViolation<Object>> validate = getValidator().validate(request);
        if (validate.size() > 0) {
            throw new GiteeRequestException(validate.iterator().next().getMessage());
        }
    }
}
